#!/usr/bin/python

import sys
import os

from operator import itemgetter

f = open(sys.argv[1], "r")

if (f == None):
    print "Arquivo invalido"
    exit(1)

pList = {}

for line in f:
    proto = line.replace("\t", " ")[0:line.find("#")].split(" ")

    while '' in proto:
        proto.remove('')
    
    if len(proto) < 2 or proto[1].find("/tcp") == -1:
        continue
    port = proto[1].split("/")[0]
    
    if int(port) > 1024:
        continue;
    
    pList[proto[0].lower()] = port
    
vname = "protos"
code = ""
i = 0

for (name, id) in sorted(pList.items(), key=itemgetter(0)):
    v = vname + "[" + str(i) + "]."
    code += v + "name = \"" + name + "\";\n" + v + "port = " + id +";\n"
    i = i+1

print code
